how to convert 24 hours to 12 hours in python

24

from datetime import datetime
d = datetime.strptime("10:30", "%H:%M")
print(d.strftime("%I:%M %p")) # outputs '10:30 AM'
d = datetime.strptime("22:30", "%H:%M")
print(d.strftime("%I:%M %p")) # outputs'10:30 PM'
df['Time'] = pd.to_datetime(df['Time'], format='%I:%M:%S %p').dt.strftime('%H:%M:%S')
print (df)
       Time
1  17:21:26
2  17:21:58
3  17:22:22
4  17:22:36
5  19:18:16

Comments

Submit
0 Comments